//
//  ActivityConfrimController.m
//  ZiJinLian
//
//  Created by 震霄 张 on 2018/4/6.
//  Copyright © 2018年 lzy. All rights reserved.
//

#import "ActivityConfrimController.h"
#import "ConfimSeatTableViewCell.h"
#import "AcvTotalPriceView.h"
#import "AddressTableViewCell.h"
#import "ZJDeliveryAddressViewController.h"
#import "AcvRemarkTableViewCell.h"
#import "AcvOrderModel.h"
#import "PayViewController.h"
@interface ActivityConfrimController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,weak)AcvTotalPriceView *totalPriceView;
@end

@implementation ActivityConfrimController

static NSString *const ZXActivityConfirmCellID = @"ZXActivityConfirmCell";
static NSString *const ZXActivityConfirmAddressCellID = @"1AddressTableViewCell";
static NSString *const ZXActivityConfirmRemarkCellID = @"acvRemarkTableViewCell";


- (void)viewDidLoad {
    [super viewDidLoad];
	[self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setupUI
{
	self.navigationItem.title = @"确认请供";
	
	UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	confirmBtn.width = screen_Width;
	confirmBtn.height = 50;
	confirmBtn.x = 0;
	confirmBtn.y = screen_Height - RD_NavBarHeight - 50;
	//	[confirmBtn setBackgroundImage:[UIImage imageNamed:@"btn_selected_s"] forState:UIControlStateNormal];
	[confirmBtn setBackgroundColor:Red_Color];
	[confirmBtn setTitle:@"确认请供" forState:UIControlStateNormal];
	confirmBtn.titleLabel.textColor = [UIColor whiteColor];
	confirmBtn.titleLabel.font = [UIFont systemFontOfSize:14];
	[confirmBtn addTarget:self action:@selector(confirmBtnCLick) forControlEvents:UIControlEventTouchUpInside];
	
	[self.view addSubview:confirmBtn];
	
	AcvTotalPriceView *priceView = [AcvTotalPriceView acvTotalPriceView];
	priceView.width = screen_Width;
	priceView.height = 50;
	priceView.x = 0;
	priceView.y = confirmBtn.y - 50;
	[self.view addSubview:priceView];
	priceView.price = self.model.payment;
	self.totalPriceView = priceView;
	
	UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, screen_Width, screen_Height-RD_NavBarHeight-100) style:UITableViewStylePlain];
	tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
	tableview.delegate = self;
	tableview.dataSource = self;
	[tableview registerNib:[UINib nibWithNibName:NSStringFromClass([ConfimSeatTableViewCell class]) bundle:nil] forCellReuseIdentifier:ZXActivityConfirmCellID];
	[tableview registerNib:[UINib nibWithNibName:@"AddressTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ZXActivityConfirmAddressCellID];
	[tableview registerNib:[UINib nibWithNibName:NSStringFromClass([AcvRemarkTableViewCell class]) bundle:nil] forCellReuseIdentifier:ZXActivityConfirmRemarkCellID];
	tableview.backgroundColor = RGB(250, 250, 250);
	[self.view addSubview:tableview];
	self.tableView = tableview;
}

-(void)confirmBtnCLick
{
	NSLog(@"confirm");
	[self makesureActOrder];

}
#pragma mark uitabledelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

	return section == 2? self.model.nodes.count:1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (indexPath.section == 0) {
		AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ZXActivityConfirmAddressCellID forIndexPath:indexPath];
		cell.model = self.model.address;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;

		return cell;
	}else if(indexPath.section == 1){
		AcvRemarkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ZXActivityConfirmRemarkCellID forIndexPath:indexPath];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.showName = [NSString stringWithFormat:@"%@(%@位)",self.model.name,self.model.quantity];
		return cell;
		
	}
	else{
		ConfimSeatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ZXActivityConfirmCellID forIndexPath:indexPath];
		cell.model = self.model.nodes[indexPath.row];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		
		return cell;
	}

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//	return indexPath.section == 0 ? 100:41;
	if (indexPath.section == 0) {
		return 100;
	}else if (indexPath.section == 1){
		return 80;
	}else{
		return 41;
	}
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	__weak typeof(self) weakSelf = self;
	if (indexPath.section == 0) {
		ZJDeliveryAddressViewController *vc=[[ZJDeliveryAddressViewController alloc]init];
		vc.styple=1;
		vc.addressBlock=^(ZJAdressModel *model){
			AcvAddressModel *addModel = [[AcvAddressModel alloc]init];
			addModel.consigneeName = model.consigneeName;
			addModel.area = model.area;
			addModel.consigneePhone = model.consigneePhone;
			addModel.detail = model.detail;
			addModel.isDefault = model.isDefault;
			addModel.userId = [model.userId integerValue];
			addModel.addressId = [model.addressId integerValue];
			weakSelf.model.address = addModel;
			[weakSelf.tableView reloadData];
			
		};
		[weakSelf.navigationController pushViewController:vc  animated:YES];
	}else if (indexPath.section == 2){
//		[ShowMainView showAcvQequestWithAction:^(NSString *name, NSString *des, BOOL open) {
//			NSLog(@"%@-%@",name,des);

//		}];
		
	    __block ActivityPositionModel *pModel = self.model.nodes[indexPath.row];
		BOOL OldOpen = pModel.isOpen>0?YES:NO;
		[ShowMainView showAcvQequest:pModel.requestName des:pModel.requestDes open:OldOpen WithAction:^(NSString *name, NSString *des, BOOL open) {
//				ActivityPositionModel *pModel = self.model.nodes[indexPath.row];
				pModel.requestName = name;
				pModel.requestDes = des;
				pModel.isOpen = open==YES?1:0;
				[weakSelf.tableView reloadData];
		}];
	}
	
}
//锁单
-(void)makesureActOrder
{
	NSMutableArray *arr = [self getNodeListJson];
	if (arr.count != self.model.nodes.count) {
		[ShowMainView showWithMsg:@"请填完愿力"];
		return;
	}
	
	NSMutableDictionary *dic=[[NSMutableDictionary   alloc]init];
	dic[@"wallId"]=[NSString stringWithFormat:@"%ld",self.ID];
	dic[@"userId"] = [ZJUserDefaultsTools getCurrentUserID];
	dic[@"buyerName"] = self.model.address.consigneeName;
	dic[@"buyerPhone"] = self.model.address.consigneePhone;
	dic[@"buyerAddress"] = [NSString stringWithFormat:@"%@%@",self.model.address.area,self.model.address.detail];
	
	dic[@"details"] = [Utils jsonFromArray:arr];
	[self doHttpRequestWithApi:API_COMFIRM_ORDER_LIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
		if ([dic[@"status"]integerValue] == 0) {
			NSLog(@"锁单成功");
			AcvOrderModel *orderM = [AcvOrderModel mj_objectWithKeyValues:dic[@"data"]];
			
			PayViewController *vc=[[PayViewController alloc]init];
			vc.orderSn = orderM.payNo;
			vc.orderDic=[dic[@"data"] copy];
			vc.isNewUrl = YES;

			[self.navigationController pushViewController:vc animated:YES];
			
		}else{
			[MBProgressHUD showError:dic[@"msg"]];
			
		}
		
	}];
	
}

-(NSMutableArray *)getNodeListJson
{
	
	NSMutableArray *arr = [NSMutableArray array];
	for (int i = 0; i<self.model.nodes.count; i++) {
		ActivityPositionModel *model = self.model.nodes[i];
		if (model.requestName.length>0) {
			NSMutableDictionary *dic = [NSMutableDictionary dictionary];
			dic[@"nodeId"] = [NSString stringWithFormat:@"%ld",model.ID];
			dic[@"userName"] = model.requestName;
			dic[@"wish"] = model.requestDes;
			dic[@"isOpen"] = [NSString stringWithFormat:@"%ld",model.isOpen];
			[arr addObject:dic];
		}
	}
	
	return arr;
}

@end
